import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Solitudefire
 * Date: 2022-08-15
 * Time: 16:38
 */
public class TestDemo2 {
    //key是关键字，val是出现的次数
    public static Map<Integer, Integer> func(int[] array) {
        Map<Integer, Integer> map = new HashMap<>();
        //判断array中的元素是否在map当中，如果不在就是1，在就是在原来的基础上加1
        for (int i = 0; i < array.length; i++) {
            if (!map.containsKey(array[i])) {
                map.put(array[i], 1);
            } else {
                Integer val = map.get(array[i]);
                map.put(array[i], val + 1);
            }
        }
        return map;
    }

    public static HashSet<Integer> func1(int[] array){
        HashSet<Integer> set = new HashSet<Integer>();
        for (int i = 0; i < array.length; i++) {
            set.add(array[i]);
        }
        return set;
    }
    public static int func2(int[] array) {
        HashSet<Integer> set = new HashSet<Integer>();
        int i = 0;
        for (; i < array.length; i++) {
            if (!set.add(array[i])){
                break;
            }else {
                set.add(array[i]);
            }
        }
        return array[i];
    }

    public static void main(String[] args) {
        int[] array = new int[10];
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(101);
        }
        HashSet<Integer> set1 = func1(array);
        System.out.println(set1);
//        int ret = func2(array);
//        System.out.println(ret);
//        Map<Integer, Integer> map = func(array);
//        System.out.println(map);
    }

    public int singleNumber(int[] nums) {
        HashSet<Integer> hashset = new HashSet<>();
        for(int i = 0; i < nums.length; i++){
            if(!hashset.contains(nums[i])){
                hashset.add(nums[i]);
            }else{
                hashset.remove(nums[i]);
            }
        }
        Object[] objects = hashset.toArray();
        return (int) objects[0];
    }

    public static void main2(String[] args) {
        Set<Integer> set = new HashSet<>();
        set.add(1);
        set.add(2);
        set.add(3);
        System.out.println(set);
        Iterator<Integer> iterator = set.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    public static void main1(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("abc", 3);
        map.put("bit", 4);//存储元素的时候，要注意key如果相同 value值会被覆盖
        map.put("hello", 2);
        map.put(null, null);
        System.out.println(map);

        Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
        for (Map.Entry<String, Integer> entry : entrySet) {
            System.out.println(entry.getKey() + "->" + entry.getValue());
        }

//        Set<String> set = map.keySet();//Set这个集合当中 存储的元素都是不重复的
//        System.out.println(set);


//        int ret = map.get("bit");//通过key 获取对应的value值
//        System.out.println(ret);
//        System.out.println(map.getOrDefault("bit2",98));
//        int ret2 = map.remove("bit");
//        System.out.println(ret2);
//        System.out.println(map);
    }
}
